package review;

import java.util.concurrent.*;

/**
 * @author 航
 * 重新系统学习并发
 * @date 2024/07/19 23:45
 **/
public class Main {

    public static void main(String[] args) throws Exception {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        Future<?> submit = executorService.submit(() -> {
            System.out.println(Thread.currentThread().getName());
            return 123;
        });
        System.out.println(submit.get());
        executorService.shutdown();

        System.out.println("--------------------");
        // Callable是个任务
        Callable<String> callable = () -> "abc";
        String call = callable.call();
        System.out.println(call);

        System.out.println("---------FutureTask-----------");
        FutureTask<?> futureTask = new FutureTask<>(callable);
        // 此时运行futureTask.get()会被阻塞，因为这个任务还没有执行
        // System.out.println("我在这里，这是我运行后的结果：" + futureTask.get());
        // 现在我要用另一个线程来执行这个任务，这样futureTask.get()就会被打印出来
        Thread thread = new Thread(futureTask);
        System.out.println("运行到这里了");
        thread.start();
        System.out.println("我在这里，这是我运行后的结果：" + futureTask.get());

        System.out.println("------------------------");
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5,
                6,
                0L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingDeque<>(),
                new ThreadPoolExecutor.CallerRunsPolicy());
        // 修改拒绝策略
        threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        threadPoolExecutor.shutdown();

        // 信号量
        Semaphore semaphore = new Semaphore(3);
        // Executor接口在Java并发编程中占据着核心的地位
        // Executor接口提供了一个标准的途径来异步执行任务，即通过execute方法提交Runnable任务。
        // 这使得任务的执行和调度与任务的定义分离，增加了代码的解耦合性和可读性。
        ExecutorService executor = Executors.newFixedThreadPool(2);
        // 下面就演示了，如果线程池没有设置拒绝策略（设置了AbortPolicy）或者使用了无界队列
        // 就可以使用信号量来控制排队的线程
        semaphore.acquire();
        try {
            executor.execute(() -> {
                try{
                    System.out.println("Thread run");
                }finally {
                    semaphore.release();
                }
            });
        } catch (RejectedExecutionException e){
            semaphore.release();
        }
        executor.shutdown();

        // 通过转换类型，来修改线程池中的属性
        ExecutorService executorService1 = Executors.newFixedThreadPool(10);
        if(executorService1 instanceof ThreadPoolExecutor){
            ((ThreadPoolExecutor) executorService1).setCorePoolSize(3);
        }else {
            System.out.println("no");
        }
        System.out.println(executorService1 instanceof ThreadPoolExecutor);
        executorService1.shutdown();
        Thread.sleep(500);

        System.out.println("------------------");
        Main m = new Main();
        System.out.println(m.hashCode());
        System.out.println(System.identityHashCode(m));


    }

    public synchronized void syTest(){
        System.out.println("syTest");
    }
}
